#pragma once

#include "BicubicUniformPeriodicBSplineSurfaces.h"
#include "Exceptions.h"

#define PI 3.1415926535897932384626433832795

namespace cagd
{
    class Sphere: public BicubicUniformPeridocBSplineSurface
    {
    public:
        Sphere(GLuint n, GLuint m, GLdouble r, GLuint u_div, GLint v_div):
                BicubicUniformPeridocBSplineSurface(n + 3, m + 3, true, true, u_div, v_div)
        {
            if (r <= 0 || n < 4 || m < 4)
                throw Exception("Invalid Sphere parameters.");

            GLdouble du = 2.0 * PI / n, dv = PI / m;
            for (GLuint i = 0; i < n+3; i++)
            {
                GLdouble u = du * i;
                for (GLuint j = 0; j < m+3; j++)
                {
                    GLdouble v = (dv * j);

                    DCoordinate& ref =  _control_net(i, j);
                    GLdouble helper = r * cos(u);
                    ref.x = helper * cos(v);
                    ref.y = helper * sin(v);
                    ref.z = r * sin(u);
                }
            }

            /*for (GLuint i = n; i < n + 3; i++)
            {
                for (GLuint j = 0; j < m; j++)
                {
                    _control_net.SetElement(i, j, _control_net(i - n, j));
                }
            }

            for (GLuint i = 0; i < n + 3; i++)
            {
                for (GLuint j = m; j < m + 3; j++)
                {
                    _control_net.SetElement(i, j, _control_net(i, j - m));
                }
            }*/
        }
    };
}
